package com.manor.im.utils;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.orhanobut.logger.Logger;
import com.tencent.imsdk.v2.V2TIMCallback;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMMessage;
import com.tencent.imsdk.v2.V2TIMSendCallback;
import com.tencent.qcloud.tim.uikit.modules.message.MessageInfo;
import com.tencent.qcloud.tim.uikit.modules.message.MessageInfoUtil;

import org.greenrobot.eventbus.EventBus;

import com.manor.common.CommonAppConfig;
import com.manor.common.bean.ChargeSuccessBean;
import com.manor.common.bean.ChatAnchorParam;
import com.manor.common.bean.ChatAudienceParam;
import com.manor.common.bean.UserBean;
import com.manor.common.event.MatchSuccessEvent;
import com.manor.common.utils.DateUtil;
import com.manor.common.utils.KLog;
import com.manor.common.utils.RouteUtil;
import com.manor.common.utils.SpUtil;
import com.manor.im.event.ChatLiveImEvent;
import com.manor.im.event.SystemMsgEvent;

import static com.tencent.qcloud.tim.uikit.modules.message.ChatUtilInfo.ACTION_ANC_ACCPET;
import static com.tencent.qcloud.tim.uikit.modules.message.ChatUtilInfo.ACTION_ANC_CANCEL;
import static com.tencent.qcloud.tim.uikit.modules.message.ChatUtilInfo.ACTION_ANC_HANG_UP;
import static com.tencent.qcloud.tim.uikit.modules.message.ChatUtilInfo.ACTION_ANC_PUSH;
import static com.tencent.qcloud.tim.uikit.modules.message.ChatUtilInfo.ACTION_ANC_REFUSE;
import static com.tencent.qcloud.tim.uikit.modules.message.ChatUtilInfo.ACTION_ANC_START;
import static com.tencent.qcloud.tim.uikit.modules.message.ChatUtilInfo.ACTION_AUD_ACCPET;
import static com.tencent.qcloud.tim.uikit.modules.message.ChatUtilInfo.ACTION_AUD_CANCEL;
import static com.tencent.qcloud.tim.uikit.modules.message.ChatUtilInfo.ACTION_AUD_HANG_UP;
import static com.tencent.qcloud.tim.uikit.modules.message.ChatUtilInfo.ACTION_AUD_PUSH;
import static com.tencent.qcloud.tim.uikit.modules.message.ChatUtilInfo.ACTION_AUD_REFUSE;
import static com.tencent.qcloud.tim.uikit.modules.message.ChatUtilInfo.ACTION_AUD_START;
import static com.tencent.qcloud.tim.uikit.modules.message.ChatUtilInfo.ACTION_CAMERA;
import static com.tencent.qcloud.tim.uikit.modules.message.ChatUtilInfo.ACTION_MATCH;

/**
 * Created by cxf on 2019/4/19.
 */

public class ChatLiveImUtil {

    private static final String IM_SYS_NOTICE = "sysnotice";//系统通知
    public static final String IM_CHAT_CALL = "call";//通话消息
    public static final String IM_CHAT_HANDLE = "livehandle";//通话中的操作
    public static final String IM_GIFT = "sendgift";//礼物
    public static final String IM_CHARGE = "charge";//充值
    public static final String GIFT_CUSTOM_TIP = "customTip";//撩一下提示
    public static final String IM_TREASURE = "mission";//任务宝箱



    public static final String METHOD = "method";
    private static final String ACTION = "action";
    private static final String SESSION_ID = "showid";
    private static final String CHAT_TYPE = "type";
    private static final String CONTENT = "content";
    private static final String AVATAR = "avatar";
    private static final String USER_NAME = "user_nickname";
    private static final String UID = "id";
    private static final String PULL = "pull";
    private static final String PUSH = "push";
    private static final String CHAT_PRICE = "total";
    private static final String LEVEL_ANCHOR = "level_anchor";


    public static void onNewMessage(JSONObject obj, String senderId) {
        if (obj == null) {
            return;
        }
        Logger.d("onNewMessage obj = --->"+obj+", senderId == "+senderId);
        switch (obj.getString(METHOD)) {
            case IM_SYS_NOTICE:
                //系统通知，全局推送，无意义（需要给UID，目前废弃）
                //onSystemNotice();
                break;
            case IM_CHARGE:
                onChargeMsg(obj);
                break;
            case IM_CHAT_CALL:
                onChatCall(obj, senderId);
                break;
            case IM_CHAT_HANDLE:
                onChatHandle(obj, senderId);
                break;
            case IM_TREASURE:
                break;
        }
    }


    /**
     * 收到系统消息
     */
    private static void onSystemNotice() {
        SpUtil.getInstance().setBooleanValue(SpUtil.HAS_SYSTEM_MSG, true);
        EventBus.getDefault().post(new SystemMsgEvent());
    }

    /**
     * 收到充值消息
     */
    private static void onChargeMsg(JSONObject obj) {
        if (CommonAppConfig.getInstance().isLaunched()) {
            EventBus.getDefault().post(obj.toJavaObject(ChargeSuccessBean.class));
        }
    }





    /**
     * 聊天通话消息
     */
    private static void onChatCall(JSONObject obj, String senderId) {
        KLog.e("onChatCall senderId", senderId);
        KLog.e("onChatCall obj", obj);
        if (obj == null) {
            return;
        }
        //{"action":0,"avatar":"","id":"4763","method":"call","showid":"1591087670","type":1,"user_nickname":"花落肩头"}, senderId == 4763
        switch (obj.getIntValue(ACTION)) {
            case ACTION_AUD_START://观众发起通话
                onChatAudToAncStart(obj, senderId);
                break;
            case ACTION_AUD_CANCEL://观众取消通话
                onChatAudToAncCancel(senderId);
                break;
            case ACTION_ANC_ACCPET://主播同意接听通话
                onChatAnchorAccpet(senderId);
                break;
            case ACTION_ANC_REFUSE://主播拒绝通话
                onChatAnchorRefuse(senderId);
                break;
            case ACTION_ANC_PUSH://主播推流成功
                onChatAnchorPushSuccess(obj.getString(PULL), senderId);
                break;
            case ACTION_AUD_PUSH://观众推流成功
                onChatAudiencePushSuccess(obj.getString(PULL), senderId);
                break;
            case ACTION_AUD_HANG_UP://观众挂断通话
                onChatAudienceHangUp(senderId);
                break;
            case ACTION_ANC_HANG_UP://主播挂断通话
                onChatAnchorHangUp(senderId);
                break;
            case ACTION_ANC_START://主播发起通话
                onChatAncToAndStart(obj, senderId);
                break;
            case ACTION_AUD_REFUSE://观众拒绝通话
                onChatAudienceRefuse(senderId);
                break;
            case ACTION_ANC_CANCEL://主播取消通话
                onChatAnchorCancel(senderId);
                break;
            case ACTION_AUD_ACCPET://观众接听通话
//                onChatAudienceAccpet(senderId);
                onChatAnchorAccpet(senderId);
                break;
            case ACTION_MATCH://匹配成功
                onMatchSuccess(obj);
                break;
        }
    }

    /**
     * 聊天通话消息,观众打开或关闭摄像头
     */
    private static void onChatHandle(JSONObject obj, String senderId) {
        KLog.e("ChatLiveImUtil-onChatHandle", "聊天通话消息,观众打开或关闭摄像头");
        if (obj == null) {
            return;
        }
        onChatAudienceCamera(obj.getByteValue(ACTION) == 2, senderId);
    }




    private static void sendCustomMsg(String info,String toUid){
        sendCustomMsg(info, toUid, true);
    }


    private static void sendCustomMsg(String info, String toUid, final boolean save) {
        final MessageInfo msgInfo = MessageInfoUtil.buildCustomMessage(info);
        V2TIMManager.getMessageManager().sendMessage(msgInfo.getTimMessage(), toUid , null, V2TIMMessage
                .V2TIM_PRIORITY_DEFAULT,false,null, new V2TIMSendCallback<V2TIMMessage>() {
            @Override
            public void onError(int i, String s) {
                    if (!save)
                        removeMsg(msgInfo.getTimMessage());
            }

            @Override
            public void onSuccess(V2TIMMessage timMessage) {
                if (!save)
                    removeMsg(msgInfo.getTimMessage());
            }

            @Override
            public void onProgress(int i) {

            }

        });
    }

    private static void removeMsg(V2TIMMessage msg){
        V2TIMManager.getMessageManager().deleteMessageFromLocalStorage(msg, new V2TIMCallback() {
            @Override
            public void onError(int i, String s) {
            }

            @Override
            public void onSuccess() {
            }
        });
    }

    /**
     * 观众向主播发起通话
     *
     * @param toUid         对方的id 即主播的id
     * @param chatSessionId 通话的会话id
     * @param chatType      通话类型
     */
    public static void chatAudToAncStart(String toUid, String chatSessionId, int chatType) {
        UserBean u = CommonAppConfig.getInstance().getUserBean();
        if (u == null) {
            return;
        }
        JSONObject obj = new JSONObject();
        obj.put(METHOD, IM_CHAT_CALL);
        obj.put(ACTION, ACTION_AUD_START);
        obj.put(UID, u.getId());
        obj.put(USER_NAME, u.getUserNiceName());
        obj.put(AVATAR, u.getAvatar());
        obj.put(SESSION_ID, chatSessionId);
        obj.put(CHAT_TYPE, chatType);
        //ImMessageUtil.getInstance().sendCustomMessage(toUid, obj.toJSONString(), false);
        sendCustomMsg(obj.toJSONString(), toUid, false);
    }


    /**
     * 主播向观众发起通话
     *
     * @param toUid         对方的id 即观众的id
     * @param chatSessionId 通话的会话id
     * @param chatType      通话类型
     * @param price         通话价格
     */
    public static void chatAncToAudStart(String toUid, String chatSessionId, int chatType, String price) {
        UserBean u = CommonAppConfig.getInstance().getUserBean();
        if (u == null) {
            return;
        }
        JSONObject obj = new JSONObject();
        obj.put(METHOD, IM_CHAT_CALL);
        obj.put(ACTION, ACTION_ANC_START);
        obj.put(UID, u.getId());
        obj.put(USER_NAME, u.getUserNiceName());
        obj.put(AVATAR, u.getAvatar());
        obj.put(LEVEL_ANCHOR, 0);
        obj.put(SESSION_ID, chatSessionId);
        obj.put(CHAT_TYPE, chatType);
        obj.put(CHAT_PRICE, price);
        Logger.e("chatAncToAudStart obj"+ obj+",uid=="+u.getId());
//        ImMessageUtil.getInstance().sendCustomMessage(toUid, obj.toJSONString(), false);
        sendCustomMsg(obj.toJSONString(), toUid, false);
    }

    /**
     * 观众向主播发起通话，主播收到观众主动发起的通话邀请
     */
    private static void onChatAudToAncStart(JSONObject obj, String senderId) {
        String audienceId = obj.getString(UID);
        //误差时间
        long errTime = 61 *1000L;
        long time = 0L;
        try {
            long sTime = Long.parseLong(obj.getString("showid")) * 1000;
            time = DateUtil.getNowLong() - sTime;
        }catch (Exception e){
            e.printStackTrace();
        }

        Logger.e("腾讯IM onChatAudToAncStart time = "+ time +" , errTime = "+errTime+" , cur = "+(time<errTime));
        if (!TextUtils.isEmpty(audienceId) && audienceId.equals(senderId) && time<errTime) {
            ChatAnchorParam param = new ChatAnchorParam();
            param.setSessionId(obj.getString(SESSION_ID));
            param.setChatType(obj.getIntValue(CHAT_TYPE));
            param.setAudienceID(audienceId);
            param.setAudienceAvatar(obj.getString(AVATAR));
            param.setAudienceName(obj.getString(USER_NAME));
            param.setAnchorActive(false);

            Logger.e("onChatAudToAncStart New isLaunched="+CommonAppConfig.getInstance().isLaunched()+
                    ", isFrontGround="+CommonAppConfig.getInstance().isFrontGround() );

            if (CommonAppConfig.getInstance().isLaunched()) {
                if (CommonAppConfig.getInstance().isFrontGround()) {
//                    RouteUtil.forwardAnchorActivity(param);
                    RouteUtil.forwardMainActivity(param);
                }else {
                    RouteUtil.forwardMainActivity(param);

//
                }
            } else {
                RouteUtil.forwardMainActivity(param);
            }
        }
    }


    /**
     * 主播向观众发起通话，观众收到主播主动发起的通话邀请
     */
    private static void onChatAncToAndStart(JSONObject obj, String senderId) {
        //{"action":2,"level_anchor":1,"method":"call","showid":"1586310602","total":"20","type":1}
        //        String anchorId = "109";
        String anchorId = obj.getString(UID);
        Logger.e("onChatAncToAndStart anchorId=="+anchorId);
        if (!TextUtils.isEmpty(anchorId) && anchorId.equals(senderId)) {
            ChatAudienceParam param = new ChatAudienceParam();
            param.setSessionId(obj.getString(SESSION_ID));
            param.setChatType(obj.getIntValue(CHAT_TYPE));
            param.setAnchorID(anchorId);
            param.setAnchorAvatar(obj.getString(AVATAR));
            param.setAnchorName(obj.getString(USER_NAME));
            param.setAnchorLevel(obj.getIntValue(LEVEL_ANCHOR));
            param.setAnchorPrice(obj.getString(CHAT_PRICE));
            param.setAudienceActive(false);
            if (CommonAppConfig.getInstance().isLaunched()) {
                RouteUtil.forwardAudienceActivity(param);
            } else {
                RouteUtil.forwardMainActivity(param);
            }
        }
    }

    /**
     * 观众向主播发起通话，观众主动取消通话
     *
     * @param toUid 对方的id 即主播的id
     */
    public static void chatAudToAncCancel(String toUid, int chatType) {
        JSONObject obj = new JSONObject();
        obj.put(METHOD, IM_CHAT_CALL);
        obj.put(ACTION, ACTION_AUD_CANCEL);
        obj.put(CHAT_TYPE, chatType);
//        ImMessageUtil.getInstance().sendCustomMessage(toUid, obj.toJSONString());
        sendCustomMsg(obj.toJSONString(), toUid);
    }

    /**
     * 观众向主播发起通话，主播收到观众主动取消通话的消息
     */
    private static void onChatAudToAncCancel(String senderId) {
        EventBus.getDefault().post(new ChatLiveImEvent(ACTION_AUD_CANCEL, senderId));
    }

    /**
     * 主播向观众发起通话，主播主动取消通话
     *
     * @param toUid 对方的id 即观众的id
     */
    public static void chatAncToAudCancel(String toUid, int chatType) {
        JSONObject obj = new JSONObject();
        obj.put(METHOD, IM_CHAT_CALL);
        obj.put(ACTION, ACTION_ANC_CANCEL);
        obj.put(CHAT_TYPE, chatType);
//        ImMessageUtil.getInstance().sendCustomMessage(toUid, obj.toJSONString());
        sendCustomMsg(obj.toJSONString(), toUid);
    }

    /**
     * 主播向观众发起通话，观众收到主播主动取消通话的消息
     */
    private static void onChatAnchorCancel(String senderId) {
        EventBus.getDefault().post(new ChatLiveImEvent(ACTION_ANC_CANCEL, senderId));
    }

    /**
     * 观众向主播发起通话，主播拒绝通话
     */
    public static void chatAnchorRefuse(String toUid, int chatType) {
        KLog.e("ChatLiveImUtil-chatAnchorRefuse", "观众向主播发起通话，主播拒绝通话");
        JSONObject obj = new JSONObject();
        obj.put(METHOD, IM_CHAT_CALL);
        obj.put(ACTION, ACTION_ANC_REFUSE);
        obj.put(CHAT_TYPE, chatType);
//        ImMessageUtil.getInstance().sendCustomMessage(toUid, obj.toJSONString());
        sendCustomMsg(obj.toJSONString(), toUid);
    }

    /**
     * 观众向主播发起通话，观众收到主播拒绝通话的消息
     */
    private static void onChatAnchorRefuse(String senderId) {
        KLog.e("ChatLiveImUtil-onChatAnchorRefuse", "观众向主播发起通话，观众收到主播拒绝通话的消息");
        EventBus.getDefault().post(new ChatLiveImEvent(ACTION_ANC_REFUSE, senderId));
    }

    /**
     * 主播向观众发起通话，观众拒绝通话
     */
    public static void chatAudienceRefuse(String toUid, int chatType) {
        JSONObject obj = new JSONObject();
        obj.put(METHOD, IM_CHAT_CALL);
        obj.put(ACTION, ACTION_AUD_REFUSE);
        obj.put(CHAT_TYPE, chatType);
//        ImMessageUtil.getInstance().sendCustomMessage(toUid, obj.toJSONString());
        sendCustomMsg(obj.toJSONString(), toUid);
    }

    /**
     * 主播向观众发起通话，主播收到观众拒绝通话的消息
     */
    private static void onChatAudienceRefuse(String senderId) {
        EventBus.getDefault().post(new ChatLiveImEvent(ACTION_AUD_REFUSE, senderId));
    }

    /**
     * 观众向主播发起通话，主播同意通话
     */
    public static void chatAnchorAccpet(String toUid) {
        JSONObject obj = new JSONObject();
        obj.put(METHOD, IM_CHAT_CALL);
        obj.put(ACTION, ACTION_ANC_ACCPET);
//        ImMessageUtil.getInstance().sendCustomMessage(toUid, obj.toJSONString(), false);
        sendCustomMsg(obj.toJSONString(), toUid, false);
    }

    /**
     * 观众向主播发起通话，观众收到主播同意通话的消息
     */
    private static void onChatAnchorAccpet(String senderId) {
        EventBus.getDefault().post(new ChatLiveImEvent(ACTION_ANC_ACCPET, senderId));
    }

    /**
     * 主播向观众发起通话，观众同意通话
     */
    public static void chatAudienceAccpet(String toUid) {
        JSONObject obj = new JSONObject();
        obj.put(METHOD, IM_CHAT_CALL);
        obj.put(ACTION, ACTION_AUD_ACCPET);
//        ImMessageUtil.getInstance().sendCustomMessage(toUid, obj.toJSONString(), false);
        sendCustomMsg(obj.toJSONString(), toUid, false);
    }

    /**
     * 主播向观众发起通话，主播收到观众同意通话的消息
     */
    private static void onChatAudienceAccpet(String senderId) {
        EventBus.getDefault().post(new ChatLiveImEvent(ACTION_AUD_ACCPET, senderId));
    }

    /**
     * 观众推流成功，把播放地址发给主播
     *
     * @param playUrl 观众的播放地址
     */
    public static void chatAudiencePushSuccess(String toUid, String playUrl) {
        JSONObject obj = new JSONObject();
        obj.put(METHOD, IM_CHAT_CALL);
        obj.put(ACTION, ACTION_AUD_PUSH);
        obj.put(PULL, playUrl);
//        ImMessageUtil.getInstance().sendCustomMessage(toUid, obj.toJSONString(), false);
        sendCustomMsg(obj.toJSONString(), toUid, false);
    }

    /**
     * 主播收到观众推流成功，发过来的播流地址
     *
     * @param andiencePlayUrl 观众的播放地址
     */
    private static void onChatAudiencePushSuccess(String andiencePlayUrl, String senderId) {
        ChatLiveImEvent e = new ChatLiveImEvent(ACTION_AUD_PUSH, senderId);
        e.setAudiencePlayUrl(andiencePlayUrl);
        EventBus.getDefault().post(e);
    }


    /**
     * 主播推流成功，把播放地址发给观众
     *
     * @param playUrl 观众的播放地址
     */
    public static void chatAnchorPushSuccess(String toUid, String playUrl) {
        JSONObject obj = new JSONObject();
        obj.put(METHOD, IM_CHAT_CALL);
        obj.put(ACTION, ACTION_ANC_PUSH);
        obj.put(PULL, playUrl);
//        ImMessageUtil.getInstance().sendCustomMessage(toUid, obj.toJSONString(), false);
        sendCustomMsg(obj.toJSONString(), toUid, false);
    }


    /**
     * 观众收到主播推流成功，发过来的播流地址
     *
     * @param anchorPlayUrl 主播的播放地址
     */
    private static void onChatAnchorPushSuccess(String anchorPlayUrl, String senderId) {
        ChatLiveImEvent e = new ChatLiveImEvent(ACTION_ANC_PUSH, senderId);
        e.setAnchorPlayUrl(anchorPlayUrl);
        EventBus.getDefault().post(e);
    }


    /**
     * 观众在通话过程中，主动挂断通话
     *
     * @param toUid 对方的id 即主播的id
     */
    public static void chatAudienceHangUp(String toUid, int chatType, String chatDuration) {
        KLog.e("ChatLiveImUtil-chatAudienceHangUp", "观众在通话过程中，主动挂断通话");
        JSONObject obj = new JSONObject();
        obj.put(METHOD, IM_CHAT_CALL);
        obj.put(ACTION, ACTION_AUD_HANG_UP);
        obj.put(CHAT_TYPE, chatType);
        obj.put(CONTENT, chatDuration);
//        ImMessageUtil.getInstance().sendCustomMessage(toUid, obj.toJSONString());
        sendCustomMsg(obj.toJSONString(), toUid);
    }


    /**
     * 主播在通话过程中，收到观众主动挂断通话的消息
     */
    private static void onChatAudienceHangUp(String senderId) {
        KLog.e("ChatLiveImUtil-onChatAudienceHangUp", "主播在通话过程中，收到观众主动挂断通话的消息");
        EventBus.getDefault().post(new ChatLiveImEvent(ACTION_AUD_HANG_UP, senderId));
    }


    /**
     * 主播在通话过程中，主动挂断通话
     *
     * @param toUid 对方的id 即观众的id
     */
    public static void chatAnchorHangUp(String toUid, int chatType, String chatDuration) {
        KLog.e("ChatLiveImUtil-chatAnchorHangUp", "主播在通话过程中，主动挂断通话");
        JSONObject obj = new JSONObject();
        obj.put(METHOD, IM_CHAT_CALL);
        obj.put(ACTION, ACTION_ANC_HANG_UP);
        obj.put(CHAT_TYPE, chatType);
        obj.put(CONTENT, chatDuration);
//        ImMessageUtil.getInstance().sendCustomMessage(toUid, obj.toJSONString());
        sendCustomMsg(obj.toJSONString(), toUid);
    }

    /**
     * 观众在通话过程中，收到主播主动挂断通话的消息
     */
    private static void onChatAnchorHangUp(String senderId) {
        String uid = CommonAppConfig.getInstance().getUid();

        KLog.e("ChatLiveImUtil-onChatAnchorHangUp", "观众在通话过程中，收到主播主动挂断通话的消息"
                +",senderId = "+senderId+",uid = "+uid);

        EventBus.getDefault().post(new ChatLiveImEvent(ACTION_ANC_HANG_UP, senderId));
    }


    /**
     * 观众在通话过程中，打开和关闭摄像头
     *
     * @param openCamera true打开 false关闭
     * @param toUid      对方的id 即主播的id
     */
    public static void chatAudienceCamera(boolean openCamera, String toUid) {
        KLog.e("ChatLiveImUtil-chatAudienceCamera", "观众在通话过程中，打开和关闭摄像头"
                +",openCamera = "+openCamera+",toUid = "+toUid);
        JSONObject obj = new JSONObject();
        obj.put(METHOD, IM_CHAT_HANDLE);
        obj.put(ACTION, openCamera ? 2 : 1);
//        ImMessageUtil.getInstance().sendCustomMessage(toUid, obj.toJSONString(), false);
        sendCustomMsg(obj.toJSONString(), toUid, false);
    }

    /**
     * 主播在通话过程中，收到观众打开和关闭摄像头的消息
     */
    private static void onChatAudienceCamera(boolean openCamera, String senderId) {
        ChatLiveImEvent e = new ChatLiveImEvent(ACTION_CAMERA, senderId);
        e.setAudienceCameraOpen(openCamera);
        EventBus.getDefault().post(e);
    }

    /**
     * 匹配成功
     */
    private static void onMatchSuccess(JSONObject obj) {
        Logger.e("onMatchSuccess obj =="+obj);
        UserBean u = CommonAppConfig.getInstance().getUserBean();
        if (u == null || !CommonAppConfig.getInstance().isLaunched()) {
            return;
        }
        Logger.e("onMatchSuccess hasAuth == "+ u.hasAuth());

        if (!u.getSex().equals("1")) {
            ChatAnchorParam param = new ChatAnchorParam();
            param.setSessionId(obj.getString(SESSION_ID));
            param.setChatType(obj.getIntValue(CHAT_TYPE));
            param.setAudienceID(obj.getString(UID));
            param.setAudienceAvatar(obj.getString(AVATAR));
            param.setAudienceName(obj.getString(USER_NAME));
            param.setAnchorPushUrl(obj.getString(PUSH));
            param.setAnchorPlayUrl(obj.getString(PULL));
            param.setAnchorActive(false);
            param.setMatch(true);
            RouteUtil.forwardAnchorActivity(param);
        } else {
            ChatAudienceParam param = new ChatAudienceParam();
            param.setSessionId(obj.getString(SESSION_ID));
            param.setChatType(obj.getIntValue(CHAT_TYPE));
            param.setAnchorID(obj.getString(UID));
            param.setAnchorAvatar(obj.getString(AVATAR));
            param.setAnchorName(obj.getString(USER_NAME));
            param.setAnchorLevel(obj.getIntValue(LEVEL_ANCHOR));
            param.setAudiencePushUrl(obj.getString(PUSH));
            param.setAudiencePlayUrl(obj.getString(PULL));

            param.setAudienceActive(false);
            param.setMatch(true);
            RouteUtil.forwardAudienceActivity(param , true);
        }

        EventBus.getDefault().post(new MatchSuccessEvent());
    }
}
